package cn.tonyandmoney.baidu.asr.event

import cn.tonyandmoney.baidu.asr.entity.NluResult
import cn.tonyandmoney.baidu.asr.recognizer.IRecogListener
import cn.tonyandmoney.baidu.asr.recognizer.RecogResult
import cn.tonyandmoney.baidu.bus.SpeechBus

/**
 * 发射事件
 */
open class EventRecogCallback : IRecogListener {

    override fun onAsrReady() = post(AsrStateEvent.READY)
    override fun onAsrBegin() = post(AsrStateEvent.BEGIN)
    override fun onAsrEnd() = post(AsrStateEvent.END)
    override fun onAsrExit() = post(AsrStateEvent.EXIT)

    override fun onAsrFinalResult(results: Array<String>?, recogResult: RecogResult, result: NluResult) {
        post(AsrResultEvent(AsrResultEvent.CODE_FINAL, results, recogResult, result))
    }

    override fun onAsrPartialResult(results: Array<String>?, recogResult: RecogResult, nluResult: NluResult) {
        post(AsrResultEvent(AsrResultEvent.CODE_PARTIAL, results, recogResult, nluResult))
    }

    override fun onAsrFinish(recogResult: RecogResult) {
        post(AsrResultEvent(AsrResultEvent.CODE_FINAL, null, recogResult, NluResult()))
    }

    override fun onAsrFinishError(errorCode: Int, subErrorCode: Int, errorMessage: String?, descMessage: String?, recogResult: RecogResult?) {
        post(AsrResultEvent(AsrResultEvent.CODE_FINAL, arrayOf(subErrorCode.toString(), errorMessage
                ?: "", descMessage ?: ""),
                recogResult ?: RecogResult(), NluResult()))
    }

    override fun onAsrLongFinish() {

    }

    /**
     * 在线语义识别，我不知道这个
     */
    override fun onAsrOnlineNluResult(nluResult: String?) {

    }

    override fun onAsrVolume(volumePercent: Int, volume: Int) {

    }

    override fun onAsrAudio(data: ByteArray, offset: Int, length: Int) = post(AsrAudioEvent(data, offset, length))
    override fun onOfflineLoaded() = post(AsrOfflineEvent.LOADED)
    override fun onOfflineUnLoaded() = post(AsrOfflineEvent.UN_LOADED)


    protected fun post(event: Any) = SpeechBus.post(event)


}
